﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace IceCS.Utility
{
    /// <summary>
    /// 文本工具函数
    /// </summary>
    /// <example>
    /// 
    /// </example>
    public class TextUtility
    {
        /// <summary>
        /// 转换html为纯文本
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        /// <example>
        /// TextUtility.ConvertHtml2PlainText("<html><head><title>Doc</title><body><div>content1</div><span>content2</span></body></head></html>")
        /// </example>
        public static string ConvertHtml2PlainText(string html)
        {
            if (string.IsNullOrWhiteSpace(html))
            {
                html = "";
            }
            html = Regex.Replace(html, "(<.+?>|\n)*", "");
            html = Regex.Replace(html, "&\\w+;", "");
            return html;
        }

        /// <summary>
        /// 截断文本为指定长度
        /// </summary>
        /// <param name="text"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Substring(string text, int length = 0)
        {
            if (text.Length < length)
            {
                return text;
            }
            return text.Substring(0, length);
        }

        /// <summary>
        /// 获取所有ascii列表值集合
        /// </summary>
        /// <returns></returns>
        /// <remarks>
        /// 减号 -(45)
        /// 数字 0(48)-9(57)
        /// 大写字母 A(65)-Z(90)
        /// 小写字母 a(97)-z(122)
        /// </remarks>
        public static List<KeyValuePair<int, char>> GetASCIIList()
        {
            List<KeyValuePair<int, char>> list = new();
            for (int i = 1; i < 128; i++)
            {
                list.Add(new KeyValuePair<int, char>(i, (char)i));
            }
            return list;
        }

        /// <summary>
        /// 转换字符串为数字,其中非数字符号会过滤掉只会包含0-9的字符后再转换,如 fx005x3会转换为0053，
        /// 如果第一个字符是 - ,会一并保留
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        /// <remarks>
        /// 减号 -(45)
        /// 数字 0(48)-9(57)
        /// </remarks>
        /// <example>
        /// var r1 = TextUtility.ConvertStringToNumber("");
        /// var r2 = TextUtility.ConvertStringToNumber("-578");
        /// var r3 = TextUtility.ConvertStringToNumber("546");
        /// var r4 = TextUtility.ConvertStringToNumber("0x00g66");
        /// var r5 = TextUtility.ConvertStringToNumber("-good5695ben");
        /// </example>
        public static int ConvertStringToNumber(string text)
        {
            if (text.Length == 0)
            {
                return 0;
            }
            //过滤非 0-9 - 字符
            var text2 = "";
            //是否是负数
            var isNegative = false;
            // - 负号
            if (text[0] == 45)
            {
                isNegative = true;
                text = text[1..];
            }
            for (int i = 0; i < text.Length; i++)
            {
                var c = text[i];
                if (c >= 48 && c <= 57)
                {
                    text2 += text[i];
                }
            }
            if (text2.Length == 0)
            {
                return isNegative ? -0 : 0;
            }
            int num = 0;
            int carry = 1;//进位值，1 10 100 1000
            for (int i = text2.Length - 1; i >= 0; i--)
            {
                //ascii 码 48 对应数字 0
                int realNum = text2[i] - 48;
                num += carry * realNum;
                carry *= 10;
            }
            return isNegative ? -num : num;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string ConvertNumberToString(string text)
        {
            return "";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string ConvertStringToUpper(string text)
        {
            return text.ToUpper();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string ConvertStringToLower(string text)
        {
            return text.ToLower();
        }

        /// <summary>
        /// 转换为首字母大写
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        /// <example>
        /// TextUtility.ConvertStringToCaption("   you are who , who care you")
        /// </example>
        public static string ConvertStringToCaption(string text)
        {
            text = text.Trim();
            var nextCharToUpper = true;
            var text2 = "";
            for (int i = 0; i < text.Length; i++)
            {
                var c = text[i];
                if (nextCharToUpper)
                {
                    c = char.ToUpper(c);
                    nextCharToUpper = false;
                }
                //空格
                if (c == 32)
                {
                    nextCharToUpper = true;
                }
                text2 += c;
            }
            return text2;
        }

        /// 倒序输出文本
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string Reverse(string text)
        {
            var charArr = text.ToCharArray();
            Array.Reverse(charArr);
            return new String(charArr);
        }
    }
}
